import pyperclip
import opencc


def process_clipboard():
    # 读取剪贴板内容
    clipboard_content = pyperclip.paste()
    print(f"len of clipboard_content : {len(clipboard_content)}")
    # 按行分割
    lines = clipboard_content.splitlines()

    # 寻找"論壇"和"你的回應"的位置
    start_idx = None
    end_idx = None

    for i, line in enumerate(lines):
        if "論壇" == line and start_idx is None:
            start_idx = i + 3
        if "你的回應" == line and start_idx is not None:
            end_idx = i - 1
            break
    print(f"len of lines : {i}")
    # 裁剪内容（不包括标记行）
    if start_idx is not None and end_idx is not None and end_idx > start_idx:
        content_to_convert = "\n".join(lines[start_idx + 1:end_idx])
    else:
        # 如果找不到标记，使用全部内容
        content_to_convert = clipboard_content

    # 转换为简体中文
    cc = opencc.OpenCC('tw2s')
    simplified_text = cc.convert(content_to_convert)

    # 去除空行
    cleaned_lines = [line for line in simplified_text.splitlines()
                     if line.strip() != '']
    result = "\n".join(cleaned_lines)

    # 保存
    with open("output.txt", "w", encoding='utf-8') as f:
        f.write(result)


if __name__ == "__main__":
    process_clipboard()
